<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <!-- 名字区组件 -->
          <GoodsName :goods="goods" />
          <!-- Sku组件 -->
          <GoodsSku :goods="goods" skuId="1379052171441475585" @change="changeSku" />
          <!-- 数量组件 -->
          <XtxNumbox v-model="num" :max="goods.inventory" label="数量" />
          <!-- 购物车组件 -->
          <XtxButton @click="insertCart" type="primary" style="margin-top: 20px; margin-left: 10px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot />
          <GoodsHot :typeNum="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { provide, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsName from './components/goods-name.vue'
import GoodsRelevant from './components/goods-relevant'
import GoodsSales from './components/goods-sales.vue'
import GoodsSku from './components/goods-sku.vue'
import { useGoods } from './hooks/useGoods'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
import { useRoute } from 'vue-router'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup() {
    // 获取商品详情，渲染面包屑
    const goods = useGoods()
    provide('goods', goods)

    // 点击sku组件的回调
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.inventory = sku.inventory
      }
      currsku.value = sku
    }

    // 要购买的数量
    const num = ref(1)
    const currsku = ref(null)
    const store = useStore()
    const insertCart = () => {
      if (currsku.value && currsku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currsku.value
        const { id, name, price, mainPictures } = goods.value
        store
          .dispatch('cart/insertCart', {
            skuId,
            attrsText,
            stock,
            id,
            name,
            price,
            nowPrice: price,
            picture: mainPictures[0],
            selected: true,
            isEffective: true,
            count: num.value
          })
          .then(() => {
            Message({ type: 'success', text: '加入购物车成功' })
          })
      } else {
        Message({ text: '请选择完整规格' })
      }
    }

    const route = useRoute()
    watch(
      () => route.params.id,
      () => {
        num.value = 1
      },
      { immediate: true }
    )
    return { goods, changeSku, num, insertCart }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
